import { login, getUserInfo, getUserDetailById } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    setToken(state, tokenValue) {
      state.token = tokenValue
      setToken(tokenValue)
    },
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo
    },
    removeUserInfo(state) {
      state.token = ''
      state.userInfo = {}
      removeToken()
    }
  },
  actions: {
    async fetchLogin({ commit }, form) {
      const res = await login(form)
      commit('setToken', res)
    },
    async fetchUserInfo({ commit }) {
      const res = await getUserInfo()
      const res_ = await getUserDetailById(res.userId)
      commit('setUserInfo', { ...res, ...res_ })
    }
  }
}
